package com.hafu.view.RuKu;
import com.hafu.model.GysInfo;
import com.hafu.model.RuKuDetail;
import com.hafu.model.RuKuMain;
import com.hafu.model.SpInfo;
import com.hafu.service.GysInfoService;
import com.hafu.service.RuKuService;
import com.hafu.service.SpInfoService;
import com.hafu.util.StringUtil;
import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;
public class AddRuKuFrm extends JFrame {
    private GysInfoService gysInfoService = new GysInfoService();
    private SpInfoService spInfoService = new SpInfoService();
    private RuKuService ruKuService = new RuKuService();
    private TableColumn tableColumn;
    public AddRuKuFrm() {
        initComponents();
        fillComboBox();
    }
    private void fillComboBox() {
        ArrayList<GysInfo> gysInfoByName = gysInfoService.getGysInfoByName(new GysInfo());
        for (GysInfo gysInfo : gysInfoByName) {
            comboBox1.addItem(gysInfo.getName());
        }
        JComboBox<SpInfo> comboBox = new JComboBox<>();
        ArrayList<SpInfo> spInfoByName = spInfoService.getSpInfoByName(new SpInfo());
        for (SpInfo spInfo : spInfoByName) {
            comboBox.addItem(spInfo);
        }
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        tableColumn = table1.getColumnModel().getColumn(0);
        tableColumn.setCellEditor(new DefaultCellEditor(comboBox));
    }
    private void button1(ActionEvent e) {
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        defaultTableModel.addRow(new Vector());
    }
    private void button2(ActionEvent e) {
        RuKuMain ruKuMain = new RuKuMain();
        ruKuMain.setRukuName(textField1.getText());
        ruKuMain.setGysName(comboBox1.getSelectedItem().toString());
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        int rowCount = defaultTableModel.getRowCount();
        ArrayList<RuKuDetail> rukudetailList = new ArrayList<>();
        BigDecimal totalPrice = new BigDecimal(0);
        for (int i = 0; i < rowCount; i++) {
            SpInfo SpInfo1 = (SpInfo)table1.getValueAt(i, 0);
            if (SpInfo1 == null){
                continue;
            }
            if (StringUtil.isEmpty(SpInfo1.getName())){
                continue;
            }
            String s = table1.getValueAt(i, 1).toString();
            Integer num = Integer.parseInt(s);
            RuKuDetail ruKuDetail = new RuKuDetail();
            ruKuDetail.setSpName(SpInfo1.getName());
            ruKuDetail.setNum(num);
            ruKuDetail.setPrice(SpInfo1.getPrice());
            rukudetailList.add(ruKuDetail);
            totalPrice=totalPrice.add(SpInfo1.getPrice().multiply(new BigDecimal(num)));
        }
        ruKuMain.setRukudetailList(rukudetailList);
        ruKuMain.setPrice(totalPrice);
        boolean b = ruKuService.addRuKu(ruKuMain);
        if (b) {
            JOptionPane.showMessageDialog(null, "添加成功");
            this.dispose();
        }else {
            JOptionPane.showMessageDialog(null, "添加失败");
        }
    }
    private void initComponents() {
        label1 = new JLabel();
        textField1 = new JTextField();
        comboBox1 = new JComboBox();
        button1 = new JButton();
        button2 = new JButton();
        label2 = new JLabel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        setTitle("\u6dfb\u52a0\u5165\u5e93\u5355");
        Container contentPane = getContentPane();
        label1.setText("\u5165\u5e93\u540d\u79f0");
        button1.setText("\u6dfb\u52a0\u65b0\u884c");
        button1.addActionListener(e -> button1(e));
        button2.setText("\u63d0\u4ea4\u5165\u5e93");
        button2.addActionListener(e -> button2(e));
        label2.setText("\u4f9b\u5e94\u5546\u9009\u62e9");
        {
            table1.setModel(new DefaultTableModel(
                    new Object[][] {
                            {null, null},
                    },
                    new String[] {
                            "\u5546\u54c1", "\u6570\u91cf"
                    }
            ));
            scrollPane1.setViewportView(table1);
        }
        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(35, 35, 35)
                                                .addComponent(label1, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
                                                .addGap(144, 144, 144)
                                                .addComponent(label2, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
                                                .addGap(54, 54, 54)
                                                .addComponent(comboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addGap(44, 44, 44)
                                                .addComponent(button1)
                                                .addGap(67, 67, 67)
                                                .addComponent(button2))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(100, 100, 100)
                                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 630, GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(154, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(comboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button1)
                                        .addComponent(button2)
                                        .addComponent(label2)
                                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label1))
                                .addGap(49, 49, 49)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 242, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(171, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
    }
    private JLabel label1;
    private JTextField textField1;
    private JComboBox comboBox1;
    private JButton button1;
    private JButton button2;
    private JLabel label2;
    private JScrollPane scrollPane1;
    private JTable table1;
}